<template>
	<div>
    <zh-form ref="excelModelFormRef"  :model="state.form"  :rules="state.rules">
      <zh-form-input
				prop="id"
        :disabled="true"
				label="ID"
				placeholder=""
				v-model="state.form.id"
        hidden="true"
			></zh-form-input>
      
      <zh-form-input
				prop="projectId"
        :disabled="props.showFlag"
				label="所属项目"
				placeholder=""
				v-model="state.form.projectId"
			></zh-form-input>
      
      <!-- <zh-form-input
				prop="datasourceId"
        :disabled="props.showFlag"
				label="所属数据源"
				placeholder=""
				v-model="state.form.datasourceId"
			></zh-form-input> -->

      <zh-form-select
            prop="datasourceId"
            label="所属数据源"
            placeholder="单选"
            allowClear
            :options="state.apiDict"
            :fieldNames="{ label: 'dataSourcename', value: 'dataSourceid' }"
            v-model="state.form.datasourceId"
        ></zh-form-select>
      
      <zh-form-input
				prop="tableSchema"
        :disabled="props.showFlag"
				label="表模式名"
				placeholder=""
				v-model="state.form.tableSchema"
			></zh-form-input>

      <zh-form-input
				prop="tableName"
        :disabled="props.showFlag"
				label="表名"
				placeholder=""
				v-model="state.form.tableName"
			></zh-form-input>

      <zh-form-input
				prop="tableFullName"
				label="表全称"
        :disabled="props.showFlag"
				placeholder="表全称"
				v-model="state.form.tableFullName"
			></zh-form-input>

      <zh-form-select
              prop="tableType"
              label="表类型"
              :options="state.dictData.tableType"
              placeholder="请选择"
              v-model="state.form.tableType"
      ></zh-form-select>
 
          <zh-form-input prop="tableComment" label="表注释" :disabled="props.showFlag" placeholder=""
                         v-model="state.form.tableComment"></zh-form-input>
          <zh-form-input prop="topicId" label="所属主题域" :disabled="props.showFlag" placeholder=""
                         v-model="state.form.topicId"></zh-form-input>
          <zh-form-select
              prop="boxLevel"
              label="所属数仓层级" 
              :disabled="props.showFlag"
              :options="state.dictData.boxLevel"
              placeholder="请选择"
              v-model="state.form.boxLevel"
          ></zh-form-select>
          <zh-form-select
              prop="safelevel"
              label="数据安全等级"
              :disabled="props.showFlag"
              :options="state.dictData.safelevel"
              placeholder="请选择"
              v-model="state.form.safeLevel"
          ></zh-form-select>
          <zh-form-select
              prop="productFrequency"
              label="数据产生频率"
              :disabled="props.showFlag"
              :options="state.dictData.productFrequency"
              placeholder=""
              v-model="state.form.productFrequency"
          ></zh-form-select>
          <zh-form-select
              prop="etlTime"
              label="数据时效性"
              :disabled="props.showFlag"
              :options="state.dictData.etlTime"
              placeholder=""
              v-model="state.form.etlTime"
          ></zh-form-select>
          <zh-form-select
              prop="etlMode"
              label="数据采集方式"
              :disabled="props.showFlag"
              :options="state.dictData.etlMode"
              placeholder=""
              v-model="state.form.etlMode"
          ></zh-form-select>
          <zh-form-select
              prop="valid"
              label="是否有效"
              :disabled="props.showFlag"
              :options="state.dictData.valid"
              placeholder=""
              v-model="state.form.valid"
          ></zh-form-select>
          <zh-form-select
              prop="uniformity"
              label="一致性"
              :disabled="props.showFlag"
              :options="state.dictData.uniformity"
              placeholder=""
              v-model="state.form.uniformity"
          ></zh-form-select>

        
    </zh-form>
  </div>
</template>
<script lang="ts" setup name="ExcelModelForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
	modelInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
  showFlag:{
    type:Boolean,
    default:false
  },
  modelValue: Boolean,
});
//------------------定义子组件---------------------------
const excelModelFormRef=ref()
const emit = defineEmits(['update:modelValue']);
//------------------内部变量定义-------------------------
const state=reactive({
  form:{newFlag:'0'} as any,
  rules:{
    tableName: [
      {
        required: true,
        message: '请输入表名',
        trigger: 'blur',
      },
    ],
    tableFullName: [
      {
        required: true,
        message: '请输入表全称',
        trigger: 'blur',
      },
    ],
    datasourceId: [
      {
        required: true,
        message: '请输入所属数据源',
        trigger: 'blur',
      },
    ],
    
  },
  dictData: {} as any, 
  apiDict:[],
  
})
//------------------定义调用方法-------------------------

function getDetail(){
  if(!props.modelInfo||!props.modelInfo.id){
    return;
  }
  let params={id:props.modelInfo.id};
  $api.common.send.post('coTableMeta0002', params).then((res) => {
    // state.codeDisabled=true
		state.form = Object.assign({},res.result);
	});
}
async function save(){
  //校验表单
	let checkFlag = await excelModelFormRef.value.checkForm();
	if (checkFlag) {
    const info = state.form;
    let ret=' ' as any;
		if (!props.showFlag&&info.id) {
      ret=await $api.common.send.post('coTableMeta0005',info);
      ret.saveType = 'edit';
      if(ret.code === 0){
        ElMessage.success('修改成功！');
        emit('update:modelValue', false);
        console.log('修改成功 = ');

      }
    } else {
      ret=await $api.common.send.post('coTableMeta0004',info);	
      ret.saveType = 'add';
      if(ret.code === 0){
        ElMessage.success('新建成功！');
        emit('update:modelValue', false);
        console.log('新增成功 = ');
      }
		}
    return ret;
  }
	return null;
}
//------------------初始化------------------------------
onMounted(()=>{  
  getDetail();
})

const params = {
    dictFields: 'tabledatatype,primarykey,nullable,safelevel,desensitization,tableType',
  };
  $api.common.getBatchDict.post(params).then((res) => {
    state.dictData = res.result;  
  });

  $api.common.send.post('dataSource0001',{}).then((res) => {
  state.apiDict = res.result.list;
  });

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: state.form ,save});
</script>
<style lang="scss" scoped></style>
